﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Documents;

namespace SHomeWorkshop.LunarMind.Commands
{
    class CmdBindings
    {
        private static List<CommandBinding> mainWindowCmdBindingsList = new List<CommandBinding>();

        public static List<CommandBinding> MainWindowCmdBindingsList
        {
            get { return mainWindowCmdBindingsList; }
        }

        private static List<CommandBinding> scrollViewerCmdBindingsList = new List<CommandBinding>();

        public static List<CommandBinding> ScrollViewerCmdBindingsList
        {
            get { return CmdBindings.scrollViewerCmdBindingsList; }
        }

        static CmdBindings()
        {
            if (Globals.MainWindow != null)
            {
                TextBox tbxCaret = Globals.MainWindow.tbxCaret;
                ////禁止tbxCaret的内建命令。
                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.X, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.C, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.V, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.A, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.S, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.PageDown, ModifierKeys.None));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.SaveAs, null, SuppressCommand));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Z, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Y, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Delete, ModifierKeys.None));

                tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleInsert, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Insert, ModifierKeys.None));

                tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.AlignLeft, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.L, ModifierKeys.Control));

                tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.AlignRight, null, SuppressCommand));
                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.R, ModifierKeys.Control));

                tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Back, ModifierKeys.None));

                //始终无法解决PageDown和PageUp快捷键冲突的问题。不知这两个命令被挂到哪个内建命令上了。
                //PageDown/PageUp/Up/Down/Left/Right这六个键的处理都放在主窗口的previewKeyDown事件处理器中。

                //tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.MoveDownByLine, null, SuppressCommand));
                //tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Down, ModifierKeys.None));

                //tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.MoveUpByLine, null, SuppressCommand));
                //tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Up, ModifierKeys.None));

                //tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.MoveLeftByCharacter, null, SuppressCommand));
                //tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Left, ModifierKeys.None));

                //tbxCaret.CommandBindings.Add(new CommandBinding(EditingCommands.MoveRightByCharacter, null, SuppressCommand));
                //tbxCaret.InputBindings.Add(new KeyBinding(ApplicationCommands.NotACommand, Key.Right, ModifierKeys.None));


                //end_禁止tbxCaret的内建命令。
            }

            //禁用某些不需要的快捷键（例如Esc）。
            mainWindowCmdBindingsList.Add(DoNothingCommand.CmdBinding);

            //TODO: 应将所有命令都添加到此处。
            mainWindowCmdBindingsList.Add(NewDocumentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(OpenDocumentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SaveDocumentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SaveAsDocumentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(EditTitleTextCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(UndoCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(RedoCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertLowerLevelTitleCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertSameLevelTitleCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(TitleBorderWidthCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(CutCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(DeleteCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(CopyCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(PasteCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SelectAllCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(ClearTitleCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(CopyAsImageToClipBoardCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(OutportToImageFileCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(OutportToSplitedImageFileCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SwitchDirectionCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(MoveDownBetweenSameLevelCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(MoveUpBetweenSameLevelCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(MoveToLowerLevelCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(MoveToHigherLevelCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(FollowPreviewTitleCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(SetBackgroundCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetBorderColorCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetDocumentBackgroundCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetForegroundCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(EditCommentTextCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertTitleImageCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetRelationLineStrokeCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(AddRelationCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertCommentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertPictureBoxCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertFixedTitleCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SwitchTitleCollapsedCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SwitchTitleOutBorderCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(InsertSummaryCommentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetTitleIndentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetTextDockBottomCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetTextDockLeftCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetTextDockRightCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(SetTextDockTopCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(PrintDocumentCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(PrintDocumentToA4PaperCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(SetDocumentPaddingCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(CollapseAllTitlesCommand.CmdBinding);
            mainWindowCmdBindingsList.Add(ExpandAllTitlesCommand.CmdBinding);

            mainWindowCmdBindingsList.Add(EnlargeTextCommand.CmdBinding);//放大文本
            mainWindowCmdBindingsList.Add(ReduceTextCommand.CmdBinding);//缩小文本

            mainWindowCmdBindingsList.Add(SetTitleIconXamlTextCommand.CmdBinding);//粘贴Xaml文本到Title。

            //另一些命令只在主编辑区域才应该有效。
            //导图视图上下左右选取标题
            scrollViewerCmdBindingsList.Add(MindViewSelectUpCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(MindViewSelectDownCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(MindViewSelectLeftCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(MindViewSelectRightCommand.CmdBinding);

            //横向列表视图上下左右选取标题
            scrollViewerCmdBindingsList.Add(HorizontalListSelectUpCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(HorizontalListSelectDownCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(HorizontalListSelectLeftCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(HorizontalListSelectRightCommand.CmdBinding);

            //纵向列表视图上下左右选取标题
            scrollViewerCmdBindingsList.Add(VerticalListSelectUpCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(VerticalListSelectDownCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(VerticalListSelectLeftCommand.CmdBinding);
            scrollViewerCmdBindingsList.Add(VerticalListSelectRightCommand.CmdBinding);

        }

        /// <summary>
        /// 用于禁止tbxCaret的内建命令。这些命令会与自定义命令冲突。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void SuppressCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
            e.Handled = true;
        }
    }
}
